<template>
  <p>
    <a-space>
      <a-button type="primary" @click="handleQuery()">刷新</a-button>
      <a-button type="primary" @click="onAdd">新增</a-button>
    </a-space>
  </p>
  <a-table :dataSource="passengers"
           :columns="columns"
           :pagination="pagination"
           @change="handleTableChange"
           :loading="loading">
    <template #bodyCell="{ column, record }">
      <template v-if="column.dataIndex === 'operation'">
        <a-space>
          <a-popconfirm
              title="删除后不可恢复，确认删除?"
              @confirm="onDelete(record)"
              ok-text="确认" cancel-text="取消">
            <a style="color: red">删除</a>
          </a-popconfirm>
          <a @click="onEdit(record)">编辑</a>
        </a-space>
      </template>
      <template v-else-if="column.dataIndex === 'type'">
        <span v-for="item in PASSENGER_TYPE_ARRAY" :key="item.code">
          <span v-if="item.code === record.type">
            {{item.desc}}
          </span>
        </span>
      </template>
    </template>
  </a-table>
  <a-modal v-model:visible="visible" title="乘车人" @ok="handleOk"
           ok-text="确认" cancel-text="取消">
    <a-form :model="passenger" :label-col="{span: 4}" :wrapper-col="{ span: 20 }">
      <a-form-item label="姓名">
        <a-input v-model:value="passenger.name" />
      </a-form-item>
      <a-form-item label="身份证">
        <a-input v-model:value="passenger.idCard" />
      </a-form-item>
      <a-form-item label="旅客类型">
        <a-select v-model:value="passenger.type">
          <a-select-option v-for="item in PASSENGER_TYPE_ARRAY" :key="item.code" :value="item.code">
            {{item.desc}}
          </a-select-option>
        </a-select>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup>
import {ref, onMounted } from 'vue';
import {notification} from "ant-design-vue";
import axios from "axios";
const PASSENGER_TYPE_ARRAY = window.PASSENGER_TYPE_ARRAY;
const visible = ref(false);
let passenger = ref({
  id: undefined,
  memberId: undefined,
  name: undefined,
  idCard: undefined,
  type: undefined,
  createTime: undefined,
  updateTime: undefined,
});
const passengers = ref([]);
// 分页的三个属性名是固定的
const pagination = ref({
  total: 0,
  current: 1,
  pageSize: 10,
});
let loading = ref(false);
const columns = [
  {
    title: '姓名',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '身份证',
    dataIndex: 'idCard',
    key: 'idCard',
  },
  {
    title: '旅客类型',
    dataIndex: 'type',
    key: 'type',
  },
  {
    title: '操作',
    dataIndex: 'operation'
  }
];

const onAdd = () => {
  passenger.value = {};
  visible.value = true;
};

const onEdit = (record) => {
  passenger.value = window.Tool.copy(record);
  visible.value = true;
};

const onDelete = async (record) => {
  const res = await axios.delete("/member/passenger/delete/" + record.id)
  const data = res.data;
  if (data.success) {
    notification.success({description: "删除成功！"});
    handleQuery({
      page: pagination.value.current,
      size: pagination.value.pageSize,
    });
  } else {
    notification.error({description: data.message});
  }
}

const handleOk = async () => {
  const res = await axios.post("/member/passenger/save", passenger.value)
  let data = res.data;
  if (data.success) {
    notification.success({description: "保存成功！"});
    visible.value = false;
    handleQuery({
      page: pagination.value.current,
      size: pagination.value.pageSize
    });
  } else {
    notification.error({description: data.message});
  }
}

const handleQuery = async (param) => {
  if (!param) {
    param = {
      page: 1,
      size: pagination.value.pageSize
    };
  }
  loading.value = true;
  const res = await axios.get("/member/passenger/query-list", {
    params: {
      page: param.page,
      size: param.size
    }
  })
  loading.value = false;
  let data = res.data;
  if (data.success) {
    passengers.value = data.content.list;
    // 设置分页控件的值
    pagination.value.current = param.page;
    pagination.value.total = data.content.total;
  } else {
    notification.error({description: data.message});
  }
}

const handleTableChange = (pagination) => {
  // console.log("看看自带的分页参数都有啥：" + pagination);
  handleQuery({
    page: pagination.current,
    size: pagination.pageSize
  });
};

onMounted(() => {
  handleQuery({
    page: 1,
    size: pagination.value.pageSize
  });
});
</script>

<style scoped>

</style>